package com.huarun.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * @author dzm
 * @创建时间 2018-08-08 09:46
 * @Description 类描述
 * 
 */
public class VerityUtil {

	/***
	 * 
	 * @Description 手机号校检
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
		String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
		if (mobile.length() != Constant.ELEVEN) {
			return false;
		} else {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(mobile);
			boolean isMatch = m.matches();
			return isMatch;
		}
	}

	/***
	 * 
	 * @Description 邮箱校检
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email == null) {
			return false;
		}
		String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p;
		Matcher m;
		p = Pattern.compile(regEx1);
		m = p.matcher(email);
		if (m.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 电话号码验证
	 * @author ：shijing
	 * 2016年12月5日下午4:34:21
	 * @param  str
	 * @return 验证通过返回true
	 */
	public static boolean isPhone(final String str) {
		Pattern p1 = null, p2 = null;
		Matcher m = null;
		boolean b = false;
		p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
		p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
		if (str.length() > 9) {
			m = p1.matcher(str);
			b = m.matches();
		} else {
			m = p2.matcher(str);
			b = m.matches();
		}
		return b;
	}

	public static boolean isPassword(String passWord, String pwdFormat, int minLen, int maxLen) {
		if (StringUtils.isEmpty(passWord)) {
			return false;
		}
		String regEx = "[\\s\\S]*";
		if (Constant.PWD_FORMAT_NUMBER.equals(pwdFormat)) {
			regEx = "^[0-9]{" + minLen + "," + maxLen + "}$";
		}
		if (Constant.PWD_FORMAT_LETTERS.equals(pwdFormat)) {
			regEx = "^[a-zA-Z]{" + minLen + "," + maxLen + "}$";
		}
		if (Constant.PWD_FORMAT_NUMBERLETTERS.equals(pwdFormat)) {
			regEx = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{" + minLen + "," + maxLen + "}$";
		}
		if (Constant.PWD_FORMAT_NUMBERLETTERSSYMBOL.equals(pwdFormat)) {
			regEx = "^(?![0-9]+$)(?![a-zA-Z]+$)(?![!#@$%^&*()_+/?=]+$)[0-9A-Za-z!#@$%^&*()_+/?=]{" + minLen + ","
					+ maxLen + "}$";
		}
		Pattern pattern = Pattern.compile(regEx);
		return pattern.matcher(passWord).matches();
	}

}
